package org.example.server.product.controller;

import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.example.server.product.bean.CategoryBean;
import org.example.server.product.service.CategoryService;
import org.example.server.user.bean.ResultBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("category")
public class CategoryController {

    @Autowired
    private CategoryService categoryService;

    @RequestMapping("list")
    public ResultBean list(@RequestBody CategoryBean categoryBean){
        log.info("category,分类列表,请求参数:{}",categoryBean);
        try {
            PageInfo<CategoryBean> pageInfo = categoryService.list(categoryBean);
            log.info("category,分类列表,返回结果:{}",pageInfo);
            return new ResultBean(200,"success",pageInfo);
        }catch (Exception e){
            e.printStackTrace();
            return new ResultBean(500,e.getMessage(),null);
        }
    }

    @RequestMapping("save")
    public ResultBean save(@RequestBody CategoryBean categoryBean){
        log.info("category,分类保存,请求参数:{}",categoryBean);
        try {
            categoryService.save(categoryBean);
            return new ResultBean(200,"success",null);
        }catch (Exception e){
            return new ResultBean(500,e.getMessage(),null);
        }
    }

    @RequestMapping("delete")
    public ResultBean delete(@RequestBody CategoryBean categoryBean){
        log.info("category,分类删除,请求参数:{}",categoryBean);
        try {
            categoryService.delete(categoryBean);
            return new ResultBean(200,"success",null);
        }catch (Exception e){
            return new ResultBean(500,e.getMessage(),null);
        }
    }

    @RequestMapping("get")
    public ResultBean get(@RequestBody CategoryBean categoryBean){
        log.info("category,分类详情,请求参数:{}",categoryBean);
        try {
            CategoryBean category = categoryService.get(categoryBean);
            return new ResultBean(200,"success",category);
        }catch (Exception e){
            return new ResultBean(500,e.getMessage(),null);
        }
    }

    @RequestMapping("edit")
    public ResultBean update(@RequestBody CategoryBean categoryBean){
        log.info("category,分类修改,请求参数:{}",categoryBean);
        try {
            categoryService.edit(categoryBean);
            return new ResultBean(200,"success",null);
        }catch (Exception e){
            return new ResultBean(500,e.getMessage(),null);
        }
    }
}
